//
//  ContactViewController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/6/2.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "ContactViewController.h"
#import "CGroupMgr.h"
#import "CMessageMgr.h"
#import "CMessageDraftMgr.h"
#import "CMessageDraft.h"
#import "CMessageModel.h"
#import "CContactMgr.h"
#import "CContactModel.h"
#import "WZcontactTableViewCell.h"
@interface ContactViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableview;
@property (nonatomic,strong)NSMutableArray *contactList;
@property (nonatomic, strong) CContactModel *curUser;
@end

@implementation ContactViewController
-(UITableView *)tableview
{
    if (_tableview == nil) {
        _tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
    }
    return _tableview;
}
-(NSMutableArray *)contactList
{
    if (_contactList == nil) {
        _contactList = [NSMutableArray array];
    }
    return _contactList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getContactList];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)getContactList
{
    CContactMgr *conMgr = [[CContactMgr alloc] init];
    CGroupMgr *groupMgr = [[CGroupMgr alloc] init];
    self.curUser = [conMgr loadCurContact];
    NSMutableArray *arrayM = [NSMutableArray array];
    NSArray *messagearray = [[[CMessageMgr alloc] init] loadmessageList];
    NSArray *draftArray = [[[CMessageDraftMgr alloc]init] loadAllmessageDraft];
    if (draftArray.count > 0) {
        for (CMessageDraft *draft in draftArray) {
            if (draft.MessageContent.length > 0) {
                [arrayM addObject:draft];
            }
        }
    }
    if (messagearray.count > 0) {
        for (CMessageModel *message in messagearray) {
            [arrayM addObject:message];
            for (CMessageDraft *draft in draftArray) {
                if ([draft.FriendUserId isEqualToString:message.FriendUserId] && draft.MessageContent.length > 0) {
                    [arrayM removeObject:message];
                }
            }
        }
        
    }
    
    NSArray *array = [arrayM sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"TimeStamp" ascending:YES]]];
    
    [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(CMessageModel *obj, NSUInteger idx, BOOL *stop) {
        if (obj.GroupId.length > 3) {
            CGroupModel *groupModel = [groupMgr loadGroup:obj.GroupId];
            if (groupModel != nil) {
                [self.contactList addObject:groupModel];
                
            }
        } else {
            CContactModel *model = [conMgr loadContactWithUserId:obj.FriendUserId];
            if(model != nil) {
                if ([model.UserID isEqualToString:self.curUser.UserID]) {
                    model.UserName = @"我的电脑";
                }
                [self.contactList addObject:model];
            }
        }
        
    }];
    
}
#pragma mark --- tableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contactList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *NormalCellIdentifier = @"SettingNormalCell";
    WZcontactTableViewCell *normalCell = [tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
    if (normalCell == nil) {
        normalCell = [[[NSBundle mainBundle] loadNibNamed:@"WZcontactTableViewCell" owner:self options:nil] objectAtIndex:0];
    }
    normalCell.nameLabel.text = @"王二";
    return normalCell;
}
@end
